//
//  HWInfoRecommendVC.swift
//  HWanGame
//
//  Created by Apple on 2024/8/10.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
import Stevia

class HWInfoRecommendVC: HWBaseViewController {

    private var titleLabel = c_make_ex(UILabel.self)
        .text("推荐").font(17.font_semiBold).color("#17181A").end()
    private var dianImage = c_make_ex(UIImageView.self)
        .image("segement_dian").mode(.scaleAspectFill).end()
    private let searchButton = UIIconButton(style: .leading(itemSpace: 0))
    ///标签
    private var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    private var segmentedDataSource: JXSegmentedTitleDataSource!
    private var listContainerView: JXSegmentedListContainerView!
    private var httpManager = HWHomeHttpManager()
    private var dataSource: [HWHomeInfoCateModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationView.isHidden = true
        view.backgroundColor = .colorWithHexString(hexString: "#F7F8FA")
        
        loadCateData()
        
        view.subviews{
            titleLabel
            dianImage
            searchButton
            segmentedView
        }

        searchButton.style { v in
            v.iconSize = CGSize(width: 16*widthScale, height: 16*widthScale)
            v.setImage("homeSearchLogo".toImage, for: .normal)
            v.setTitle("请输入关键进行搜索", for: .normal)
            v.setTitleColor("#AEB0B3".toColor, for: .normal)
            v.contentEdgeInsets = UIEdgeInsets(top: 7, left: 6, bottom: 0, right: 0)
            v.titleLabel?.font = 12.font_medium
            v.contentHorizontalAlignment = .leading
            v.addTarget(self, action: #selector(searchButtonClick), for: .touchUpInside)
            v.backgroundColor = "#EDEEF0".toColor
            v.layer.cornerRadius = 15*widthScale
            v.layer.masksToBounds = true
        }
        
        titleLabel.leading(15*widthScale).top(.statusHeight+10*widthScale)
        dianImage.size(13*widthScale)
        dianImage.CenterX == titleLabel.CenterX
        dianImage.Top == titleLabel.Bottom - 3
        searchButton.trailing(12*widthScale).top(.statusHeight+8*widthScale).width(280*widthScale).height(30*widthScale)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = []
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = "#47494D".toColor
        segmentedDataSource.titleSelectedColor = "#2E3033".toColor
        segmentedDataSource.titleNormalFont = 14.font_semiBold!
        segmentedDataSource.titleSelectedFont = 14.font_semiBold!
        segmentedDataSource.itemSpacing = 11*widthScale
        segmentedView.dataSource = segmentedDataSource
        segmentedView.leading(0).top(CGFloat.navi_height).height(44*widthScale).trailing(0)
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#FFB444")
        indicator.indicatorHeight = 4*widthScale
        indicator.indicatorWidth = 17*widthScale
        indicator.indicatorCornerRadius = 2*widthScale
        indicator.verticalOffset = 5*widthScale
        segmentedView.indicators = [indicator]
        
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom)
        }
    }

    @objc func searchButtonClick() {
        let searchController = HWSearchController()
        searchController.modalPresentationStyle = .overFullScreen
        self.present(searchController, animated: true, completion: nil)
    }
}

//MARK: data
extension HWInfoRecommendVC {
    
    private func loadCateData(){
        
        httpManager.getHomeInfoCate { models in
            
            var titles = [String]()
            for mod in models {
                titles.append(mod.name)
            }
            self.dataSource = models
            self.segmentedDataSource.titles = titles
            self.segmentedView.reloadData()
        } failure: { errorStr in
            
        }
    }
}

//MARK: JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate
extension HWInfoRecommendVC:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        let listVC = HWInfoRecommendListVC()
        listVC.cateId = dataSource?[index].id ?? 0
        return listVC
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}
